package edu.zjucst.spb.pkg.flow.stage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.zjucst.spb.dao.mapper.StageMapper;
import edu.zjucst.spb.domain.entity.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Objects;

/**
 * @author ZlunYan
 * @description
 * @create 2023-11-30
 */

@Component("flowStageCheckComponent")
public class StageCheckComponent {

    private static final Integer AGE_THRESHOLD = 18;

    private static class STAGE_NUMBER {
        public static final Integer LEAGUE_MEMBER = 0;

    }

    public static boolean checkAdultAndLeagueMember(Stage stage) {

        Calendar nowCalendar = Calendar.getInstance();
        if (nowCalendar.before(stage.getBirthday())) {
            throw new IllegalArgumentException("The birth date is before current time, it's unbelievable");
        }

        int yearNow = nowCalendar.get(Calendar.YEAR);
        int monthNow = nowCalendar.get(Calendar.MONTH);
        int dayNow = nowCalendar.get(Calendar.DAY_OF_MONTH);
        Calendar birthCalendar = Calendar.getInstance();
        birthCalendar.setTime(stage.getBirthday());
        int yearBirth = birthCalendar.get(Calendar.YEAR);
        int monthBirth = birthCalendar.get(Calendar.MONTH);
        int dayBirth = birthCalendar.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;
        if (monthNow < monthBirth) { age--; }
        else if (monthNow == monthBirth && dayNow < dayBirth) { age--; }


        return age>=AGE_THRESHOLD && stage.getDevelopmentPhaseId().equals(STAGE_NUMBER.LEAGUE_MEMBER);
    }
}
